package app.mvp.base;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import app.mvp.base.mvp.BaseMvpPresenter;
import app.mvp.base.mvp.BaseView;
import app.mvp.utils.OnClickUtil;
import app.photolibrary.utils.XPermissionUtils;

/**
 * Created by 刘智强 on 2017/4/6.
 *
 * MVP的baseactivity
 */

public abstract class BaseActivity<P extends BaseMvpPresenter> extends AppCompatActivity implements View.OnClickListener {

    public P mPresenter;
    public Activity mActivity;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//底部导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(getLayoutResource());
        ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View parentView = contentFrameLayout.getChildAt(0);
        if (parentView != null && Build.VERSION.SDK_INT >= 14) {
            parentView.setFitsSystemWindows(true);
        }
        mActivity = this;
        initPresenter();
        attachView();
        initView();
        initListener();
        init();


    }

    public abstract int getLayoutResource();//获取布局文件的ID

    public abstract void initPresenter();//初始化presenter对象

    public abstract void initView();//初始化控件

    public abstract void initClick(View v);//点击事件的复写

    public abstract void initListener();//设置点击事件

    public abstract void attachView();//给presenter关联view

    public abstract void detachView();//解除关联

    public abstract void init();//初始化

    /**
     * 给控件添加点击监听
     * @param ids
     */
    public final void addOnClickListener(int... ids) {
        if (ids!=null&&ids.length>0){
            for (int id:ids){
                View v=findViewById(id);
                if (v!=null){
                    v.setOnClickListener(this);
                }
            }

        }


    }

    @Override
    public void onClick(View v) {
        if (OnClickUtil.fastClick()) {
            initClick(v);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        detachView();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        XPermissionUtils.onRequestPermissionsResult(mActivity,requestCode,permissions,grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
}
